import tkinter as tk
from tkinter import ttk
import subprocess
import os
import sys

class MainPage:
    def __init__(self, root):
        self.root = root
        self.root.title("Software Collection")
        self.root.geometry("800x600")
        
        # 设置样式
        style = ttk.Style()
        style.configure("Title.TLabel", font=("Arial", 24, "bold"))
        style.configure("Link.TButton", font=("Arial", 14))
        
        # 创建主框架
        main_frame = ttk.Frame(root, padding="20")
        main_frame.pack(expand=True, fill="both")
        
        # 标题
        title_label = ttk.Label(main_frame, text="Software Collection", style="Title.TLabel")
        title_label.pack(pady=20)
        
        # 创建软件列表框架
        software_frame = ttk.Frame(main_frame)
        software_frame.pack(expand=True, fill="both")
        
        # 课程管理系统
        self.create_software_button(software_frame, "Course Management System", 
                                  "A system for managing courses, students, and schedules",
                                  self.run_course_management)
        
        # 图书馆系统
        self.create_software_button(software_frame, "Library System", 
                                  "A system for managing library resources and book loans",
                                  self.run_library_system)
        
        # 社交生活系统
        self.create_software_button(software_frame, "Social Life System", 
                                  "A system for managing social activities and events",
                                  self.run_social_system)
        
    def create_software_button(self, parent, title, description, command):
        frame = ttk.Frame(parent)
        frame.pack(fill="x", pady=20)
        
        button = ttk.Button(frame, text=title, style="Link.TButton", command=command)
        button.pack(side="left", padx=10)
        
        desc_label = ttk.Label(frame, text=description, wraplength=500)
        desc_label.pack(side="left", padx=10)
        
    def run_course_management(self):
        try:
            # 获取当前文件所在目录
            current_dir = os.path.dirname(os.path.abspath(__file__))
            # 构建课程管理系统的路径
            course_dir = os.path.join(current_dir, "Course_System")
            # 切换到课程管理系统目录
            os.chdir(course_dir)
            # 运行课程管理系统
            subprocess.Popen([sys.executable, "ui.py"], 
                           creationflags=subprocess.CREATE_NEW_CONSOLE,
                           cwd=course_dir)
        except Exception as e:
            tk.messagebox.showerror("Error", f"Failed to run Course Management System: {str(e)}")
        
    def run_library_system(self):
        try:
            # 获取当前文件所在目录
            current_dir = os.path.dirname(os.path.abspath(__file__))
            # 构建图书馆系统的路径
            library_dir = os.path.join(current_dir, "Library_System")
            # 切换到图书馆系统目录
            os.chdir(library_dir)
            # 运行图书馆系统
            subprocess.Popen([sys.executable, "library_gui.py"], creationflags=subprocess.CREATE_NEW_CONSOLE)
        except Exception as e:
            tk.messagebox.showerror("Error", f"Failed to run Library System: {str(e)}")
        
    def run_social_system(self):
        try:
            # 获取当前文件所在目录
            current_dir = os.path.dirname(os.path.abspath(__file__))
            # 构建社交生活系统的路径
            social_dir = os.path.join(current_dir, "Social_Life_System")
            # 切换到社交生活系统目录
            os.chdir(social_dir)
            # 运行社交生活系统
            subprocess.Popen([sys.executable, "main.py"], creationflags=subprocess.CREATE_NEW_CONSOLE)
        except Exception as e:
            tk.messagebox.showerror("Error", f"Failed to run Social Life System: {str(e)}")

def main():
    root = tk.Tk()
    app = MainPage(root)
    root.mainloop()

if __name__ == "__main__":
    main() 